from common.k_requests import yu_requestsC
from common.l_subPublic import envData,mrjbusC



class Req_api():
    """请求接口"""

    def __init__(self):
        """
        :param domain : 域名,默认测试环境
        :author 露驿星程
        """
        self.request = yu_requestsC
        self.dic = {}                     # 存储运行时提取的数据
        self.defaultUser = mrjbusC.DefaultUser()

    def choise_domain(self,uri):
        """
        选择域名
        :param uri : 传入uri
        :author 露驿星程
        :return 返回域名 + 端口
        """
        run_type = envData.getdata()['run_type']
        port_8098 = envData.getdata()['test_8098_port']
        if run_type == 0:
            protocol = "http://"
            if uri in port_8098:
                baseUrl = envData.getdata()['test_domain'].split(":")[0]
                return protocol + baseUrl + ":" + "8098"
            return protocol + envData.getdata()['test_domain']
        elif run_type == 1:
            protocol = "https://"
            if uri in envData.getdata()['mall_domain_uri']:
                return protocol + envData.getdata()['prod_mall_domain']
            elif uri in envData.getdata()['live_domain_uri']:
                return protocol + envData.getdata()['prod_live_domain']
        elif run_type == 2:
            protocol = "https://"
            attach = "pre-"
            if uri in envData.getdata()['mall_domain_uri']:
                return protocol + attach + envData.getdata()['prod_mall_domain']
            elif uri in envData.getdata()['live_domain_uri']:
                return protocol + attach + envData.getdata()['prod_live_domain']

    def newGoodsSpuDetail(self,spuId=None,userId=None):
        """
        商品详情接口
        :param domain : 域名,默认测试环境
        :author 露驿星程
        """
        if spuId == None:
            spuId = mrjbusC.DefaultSpuId()
        else:
            spuId = spuId
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId

        uri = "/front/p/newGoodsSpu/newGoodsSpuDetail"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {}
        json['spuId'] = spuId
        json['userId'] = userId
        res = self.request.send_request(method=method, url=url, json=json, headers=userId)
        # 防止出现响应文本为乱码，方法1
        res.encoding = 'utf-8'
        # 防止出现响应文本为乱码，方法2
        # res.encoding("latin-1")
        # res.decode("utf-8")
        # response = res.text
        # print(res.status_code)
        return res

    def homePage(self,userId=None):
        """
        商城首页接口。banner、品牌、本周上新区域
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/front/p/mallHome/main"
        domain = self.choise_domain(uri)
        method = "GET"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId)
        return res

    def topProducts(self,userId=None):
        """
        商城首页平台推荐接口
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/front/p/mallHome/topProducts"
        domain = self.choise_domain(uri)
        method = "GET"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId)
        return res

    def weeklyProducts(self,userId=None):
        """
        本周上新接口
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/front/p/mallHome/weeklyProducts"
        domain = self.choise_domain(uri)
        method = "GET"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId)
        return res

    def popularProducts(self,userId=None):
        """
        口碑爆款接口
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/front/p/mallHome/popularProducts"
        domain = self.choise_domain(uri)
        method = "GET"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId)
        return res

    def subsidyProducts(self,userId=None):
        """
        会员福利社接口
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/front/p/mallHome/subsidyProducts"
        domain = self.choise_domain(uri)
        method = "GET"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId)
        return res


    def getHomeBrandDetail(self,userId=None,brandId=None):
        """
        品牌页-本月推荐接口
        :param: id 这里的 id 在 home_brand 表
        """
        run_type = envData.getdata()['run_type']
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if brandId == None and run_type == 0:
            json = {"id":"1"}
        elif brandId == None and run_type == 1:
            json = {"id": "2000"}
        else:
            json = {"id": brandId}
        uri = "/front/p/homeBrand/getHomeBrandDetail"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        return res

    def getHomeBrandGoods(self,userId=None,brandId=None):
        """
        品牌页-本月系列接口
        """
        run_type = envData.getdata()['run_type']
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if brandId == None and run_type == 0:
            json = {"brandId":mrjbusC.getBrandId()[0],"pageNo":1}
        elif brandId == None and run_type == 1:
            json = {"brandId": mrjbusC.getBrandId()[0], "pageNo": 1}
        else:
            json = {"brandId":brandId,"pageNo":1}
        # print(json)
        uri = "/front/p/homeBrand/getHomeBrandGoods"
        domain = self.choise_domain(uri)
        # print(domain)
        method = "POST"
        url = domain + uri
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        return res

    def intendedCustomer(self,userId=None,spuId=None,intendedId=None):
        """
        小程序意向客户埋点接口
        :param: intendedId 意向客户类型 1-5
        """
        if spuId == None:
            spuId = mrjbusC.DefaultSpuId()
        else:
            spuId = spuId
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if intendedId == None:
            intendedId = 2
        else:
            intendedId = intendedId
        uri = "/front/p/custom/intendedCustomer"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {"intendedId":intendedId,"type":1,"spuId":spuId}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        return res


    def sourceBySpuId(self,userId=None,spuId=None):
        """
        小程序商品素材接口
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if spuId == None:
            spuId = mrjbusC.DefaultSpuId()
        else:
            spuId = spuId
        uri = "/front/p/sourceBySpuId"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {"spuId" : spuId}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def personalInfo(self,userId=None):
        """
        小程序首页获取个人信息
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId

        uri = "/liveUser/getPersonalInfoByUserId"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {"userId":userId}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        return res

    def selectGood(self,userId=None,spuKW=None):
        """
        小程序首页搜索商品
        :param: spuKW  商品关键字
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if spuKW == None:
            spuKW = "美人计"
        else:
            spuKW = spuKW

        uri = "/front/p/newGoodsSpu/selectGoodsSpuList"
        domain = self.choise_domain(uri)
        # print(domain)
        method = "POST"
        url = domain + uri
        json = {"pageNum":1,"key":spuKW}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def hotLiveRoom(self,userId=None):
        """
        直播热门列表
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/homePage/hotLiveRoom"
        domain = self.choise_domain(uri)
        # print(domain)
        method = "POST"
        url = domain + uri
        json = {"pageNum":1}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def hpBanner(self,userId=None):
        """
        看直播页面banner
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/homePage/homePageBanner"
        domain = self.choise_domain(uri)
        # print(domain)
        method = "POST"
        url = domain + uri
        json = {"searchType":10}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def focusLiveRoom(self,userId=None):
        """
        直播关注列表
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/homePage/focusLiveRoom"
        domain = self.choise_domain(uri)
        # print(domain)
        method = "POST"
        url = domain + uri
        json = {"pageNum":1}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def hpSearch(self,userId=None,key=None):
        """
        直播页面搜索
        :param: key 超好播Id
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        if key == None:
            key = "111111"
        else:
            key = key
        uri = "/homePage/homePageSearch"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        # print(url)
        json = {"pageNum":1,"key":key,"searchType":1}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res

    def liveOpenInfo(self,userId=mrjbusC.getSaleUser()):
        """
        APP立即开播-获取开播信息
        """
        if userId == None:
            userId = self.defaultUser
        else:
            userId = userId
        uri = "/liveRoom/getOnLiveOpenInfoVo"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {}
        res = self.request.send_request(method=method, url=url, headers=userId,json=json)
        # print(res.text)
        return res



if __name__ == "__main__":
    pass
    # apis = Req_api()
    # res = apis.liveOpenInfo()
    # print(res.status_code)
    # print(res.text)